package com.lt.controller.stu;

import com.lt.dto.*;
import com.lt.service.COSFileService;
import com.lt.service.ProjectService;
import com.lt.utils.JwtUtils;
import com.lt.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;

/**
 * @Author :liteng
 * @Time : 2023/11/5 17:17
 */

@RestController
@Slf4j
@RequestMapping("/stu")
@CrossOrigin
public class ProjectController {

    private final ProjectService projectService;

    private final COSFileService cosFileService;

    public ProjectController(ProjectService projectService, COSFileService cosFileService) {
        this.projectService = projectService;
        this.cosFileService = cosFileService;
    }

    /**
     * 学生端
     * 立项审核、我的项目
     *
     * @return
     */
    @GetMapping("/manage/establish")
    public Result projectApproval() {
        List<MyProject> allProject = projectService.getAllProject();
        return Result.success(allProject);
    }

    /**
     * 项目详情
     *
     * @param projectId
     * @return
     */
    @GetMapping("/project")
    public Result projectDetail(Long projectId) {
        ProjectApply projectDetails = projectService.getProjectDetails(projectId);
        return Result.success(projectDetails);
    }

    /**
     * 根据项目编号查询立项审核未通过原因
     *
     * @param id
     * @return
     */
    @GetMapping("projectApplyFail/{id}")
    public Result showFailApplyDetails(@PathVariable("id") Long id) {
        String unPassedReason = projectService.getUnPassedReason(id);
        return Result.success(unPassedReason);
    }

    ////////////////////////////////////////////////////////////////////////////////////////////

    /**
     * 进展记录
     *
     * @return
     */
    @GetMapping("/manage/progressRecord")
    public Result progressRecord() {
        List<ProgressRecord> records = projectService.getRecords();
        return Result.success(records);
    }

    /**
     * 中期审核
     *
     * @return
     */
    @GetMapping("/manage/interim")
    public Result getInterimReview() {
        List<InterimReviewDto> review = projectService.getReview();
        return Result.success(review);
    }

    /**
     * 提交日志
     *
     * @param jwt
     * @param file
     * @param projectId
     * @return
     */
    @PostMapping("/submitLog")
    public Result submitLog(@RequestHeader(value = "token") String jwt,
                            MultipartFile file, Long projectId) {

        //操作用户num
        Long num = JwtUtils.getNumFromJwt(jwt);
        String[] id_url = cosFileService.upload(file, num);
        if (projectId != null && file != null) {
            if (id_url[0] != null) {
                projectService.submitLog(projectId, Long.valueOf(id_url[0]));
                return Result.success("提交成功");
            }
        }

        return Result.error("提交失败");
    }

    /**
     * 更改成员
     *
     * @param changeMemberDto
     * @return
     */
    @Transactional
    @PostMapping("/memberChange")
    public Result changeMember(@RequestBody ChangeMemberDto changeMemberDto) {
        if (projectService.changeMember(changeMemberDto))
            return Result.success("修改成功");
        else return Result.error("修改失败");
    }

    /**
     * 根据项目编号查询中期审核未通过原因
     *
     * @param id
     * @return
     */
    @GetMapping("/projectInterimFail/{id}")
    public Result showInterimDetails(@PathVariable("id") Long id) {
        String unPassedReason = projectService.getInterimReason(id);
        return Result.success(unPassedReason);
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////

    /**
     * 获得结项审核
     *
     * @return
     */
    @GetMapping("/manage/closingAudit")
    public Result getClosingAudit() {
        List<ClosingAuditDto> endList = projectService.getEndList();
        return Result.success(endList);
    }

    /**
     * 获得一项结项审核
     *
     * @return
     */
    @GetMapping("/manage/closingAuditOne")
    public Result getClosingOne(Long id) {
        ClosingAuditDto endList = projectService.getEndListOne(id);
        return Result.success(endList);
    }

    /**
     * 结项审核
     * 根据id获得结项详情
     *
     * @param projectId
     * @return
     */
    @GetMapping("/closingAudit")
    public Result getClosingById(Long projectId) {
//        ProjectEnd dto = projectService.getEndItem(projectId);

        return Result.success();
    }


    /**
     * 结项材料提交
     *
     * @param closingMaterial
     * @return
     */
    @PostMapping("/materialSubmit")
    public Result submitClosingMaterial(@RequestBody ClosingMaterial closingMaterial) {
//        Long projectId = closingMaterial.getProjectId();

        if (projectService.closingMaterial(closingMaterial)) {
            return Result.success("插入成功");
        } else
            return Result.error("插入失败");
//        return null;
    }

    /**
     * 更新
     * 延期结项
     *
     * @param id
     * @return
     */
    @GetMapping("/delayedReview/{id}")
    public Result delayedReview(@PathVariable String id, String reason) {
        Long aLong = Long.valueOf(id);
        return projectService.delayedReview(aLong, reason) ? Result.success("更新成功")
                : Result.error("更新失败");
//        return null;
    }

    /**
     * 获得结项审核未通过与延期结项原因
     *
     * @return
     */
    @GetMapping("/projectEndFail/{id}")
    public Result getReason(@PathVariable("id") Long id) {
        String endReason = projectService.getEndReason(id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("projectId", id);
        map.put("details", endReason);
        return new Result(1, "success", map);
    }


}
